# 基本用法
## 准备工作
"""
import requests

r = requests.get('https://www.baidu.com')
print(type(r))
print(r.status_code)
print(type(r.text))
print(r.text)
print(r.cookies)
"""
import re

import requests.cookies
import urllib3

"""
import requests

r = requests.post('http://httpbin.org/post')
r = requests.put('http://httpbin.org/put')
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/head')
r = requests.options('http://httpbin.org/options')
"""
## get请求
"""
import requests
r = requests.get('http://httpbin.org/get')
print(r.text)
"""
"""
import requests
data = {
    'name':'getmey',
    'age':22
}
r = requests.get('http://httpbin.org/get',params=data)
print(r)
print(r.text)
"""
"""
import requests
data = {
    'name':'zhubiao',
    'age':23
}
r = requests.get('http://httpbin.org/get')
print(type(r.text))
print(r.text)
print(r.json())
print(type(r.json()))
"""
"""
import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/94.0.4606.81 Safari/537.36 '
}

r = requests.get('https://www.zhihu.com/explore', headers=headers)
pattern = re.compile('explore-feed.*?question_link.*?>(.*?)</a>', re.S)
titles = re.findall(pattern, r.text)
print(titles)
"""
"""
import requests

r = requests.get('https://github.com/favicon.ico')
print(r.text)
print('-----------------------------')
print(r.content)
"""
"""
import requests

r = requests.get('https://github.com/favicon.ico')
with open('favicon.ico', 'wb') as f:
    f.write(r.content)
"""
"""
import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/94.0.4606.81 Safari/537.36 '
}

r = requests.get('https://www.zhihu.com/explore')
print(r.text)
"""
"""
import requests

data = {'name':'germey','age':'22'}
r = requests.post('http://httpbin.org/post',data=data)
print(r.text)
"""
"""
import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/94.0.4606.81 Safari/537.36 '
}

r = requests.get('http://jianshu.com', headers=headers)
print(type(r.status_code), r.status_code)
print('-----------------')
print(type(r.cookies), r.cookies)
print('-----------------')
print(type(r.headers), r.headers)
print('-----------------')
print(type(r.history), r.history)
print('-----------------')
print(type(r.url), r.url)
"""
"""
import requests

files = {'file': open('favicon.ico', 'rb')}
r = requests.post('http://httpbin.or/post', files=files)
print(r.text)
"""
# 高级用法
"""
import requests

r = requests.get('https://www.baidu.com')
print(r.cookies)
for key,value in r.cookies.items():
    print(key + '=' + value)
"""
"""
import requests

headers = {
    'Cookie': 'BIDUPSID=34D7FBFE692A90015D13104B17EB5C84; PSTM=1634270341; '
              'BAIDUID=898601A7AA1FBF9AE9EB17459425E3CD:FG=1; BD_HOME=1; BD_UPN=123253; '
              'BAIDUID_BFESS=5F7BE8092FB9A3BE867539FA597D4290:FG=1; '
              '__yjs_duid=1_0130b6c28f2bf5409c5a207865c995721634288692420; ZD_ENTRY=google; '
              '_gcl_au=1.1.529036970.1634374975; '
              '_gcl_aw=GCL.1634375763'
              '.CjwKCAjw8KmLBhB8EiwAQbqNoPmeslplTsOTOAqwEm1sqEAG9T85bgVaPkp1u2dtTTYy0emhvafq9xoCjHMQAvD_BwE; '
              '__xsptplus861=861.1.1634374975.1634375763.2%233%7Cwww.google.com.hk%7C%7C%7C%7C%23%23-OLm8b'
              '-L8lPoD4T01LlDc4Fl4ZJvGlV5%23; '
              'ab_sr=1.0.1_NjdiZGYyYmM4NzFjOWNjMjY4MGNiNDJhNjZjOTE2MjRjZGZiZGIzZDc3ZTJiYzg1MThkMDc3YmVlYmQzMmQwOTJmMTgyYmJiM2I5MWNlN2NlM2JhMzA0MjdiYjk1NTVhN2NkY2E3YzkyYTI4ZTM4OWFlY2RhMzg1YmRjN2Y2MWQ0YjQwMjRkYTcwN2Q2MTY5ZmMzNzY1YzcxODBhMWNjNA==; BDRCVFR[FhauBQh29_R]=mbxnW11j9Dfmh7GuZR8mvqV; delPer=0; BD_CK_SAM=1; PSINO=7; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=34834_34889_34448_34068_31253_34863_34711_34584_34504_34829_34874_26350_34826_34677; BA_HECTOR=0k8k0g002424050hnv1gmvu4d0q '
}

r = requests.get('https://www.baidu.com')
print(r.text)
"""
"""
Cookie = '''BIDUPSID=34D7FBFE692A90015D13104B17EB5C84; PSTM=1634270341; 
BAIDUID=898601A7AA1FBF9AE9EB17459425E3CD:FG=1; BD_HOME=1; BD_UPN=123253; 
BAIDUID_BFESS=5F7BE8092FB9A3BE867539FA597D4290:FG=1; __yjs_duid=1_0130b6c28f2bf5409c5a207865c995721634288692420; 
ZD_ENTRY=google; _gcl_au=1.1.529036970.1634374975; 
_gcl_aw=GCL.1634375763.CjwKCAjw8KmLBhB8EiwAQbqNoPmeslplTsOTOAqwEm1sqEAG9T85bgVaPkp1u2dtTTYy0emhvafq9xoCjHMQAvD_BwE; 
__xsptplus861=861.1.1634374975.1634375763.2%233%7Cwww.google.com.hk%7C%7C%7C%7C%23%23-OLm8b-L8lPoD4T01LlDc4Fl4ZJvGlV5 
%23; ab_sr=1.0 
.1_NjdiZGYyYmM4NzFjOWNjMjY4MGNiNDJhNjZjOTE2MjRjZGZiZGIzZDc3ZTJiYzg1MThkMDc3YmVlYmQzMmQwOTJmMTgyYmJiM2I5MWNlN2NlM2JhMzA0MjdiYjk1NTVhN2NkY2E3YzkyYTI4ZTM4OWFlY2RhMzg1YmRjN2Y2MWQ0YjQwMjRkYTcwN2Q2MTY5ZmMzNzY1YzcxODBhMWNjNA==; BDRCVFR[FhauBQh29_R]=mbxnW11j9Dfmh7GuZR8mvqV; delPer=0; BD_CK_SAM=1; PSINO=7; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=34834_34889_34448_34068_31253_34863_34711_34584_34504_34829_34874_26350_34826_34677; BA_HECTOR=0k8k0g002424050hnv1gmvu4d0q '''

jar = requests.cookies.RequestsCookieJar()
headers = {
    'Host': 'www.zhihu.com',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/94.0.4606.81 Safari/537.36 '
}

for cookie in Cookie.split(';'):
    key, value = cookie.split('=', 1)
    jar.set(key, value)
    r = requests.get('https://www.zhihu.com', cookies=jar, headers=headers)
    print(r.text)
"""
"""
import requests

requests.get('http://httpbin.org/cookies/set/number/123456789')
r = requests.get('http://httpbin.org/cookies')
print(r.text)
"""
"""
import requests

s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
r = s.get('http://httpbin.org/cookies')
print(r.text)
"""
"""
import requests

response = requests.get('https://www.12306.cn')
print(response.status_code)
"""
"""
import requests

response = requests.get('https://12306.cn',verify=False)
print(response.status_code)
"""
"""
import requests
from requests.packages import urllib3

urllib3.disable_warnings()
response = requests.get('https://www.12306.cn', verify=False)
print(response.status_code)
"""
"""
import requests

proxies = {
    'http':'http://10.10.1.10:3128',
    'https':'http://10.10.1.10.1080'
}

try:
    requests.get('https://www.taobao.com', proxies=proxies)
except:
    print('proxies are wrong')
"""
"""
import requests
from requests_oauthlib import OAuth1

url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',
              'USER_OAUTH_TOKEN', 'USER_OAUTH_SECRET'
              )
response = requests.get(url, auth=auth)
print(response.text)
"""
"""
from requests import Request, Session

url = 'http://httpbin.org/post'

data = {
    'name':'germey'
}

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/94.0.4606.81 Safari/537.36 '
}

s = Session()
req = Request('POST',url,data=data,headers=headers)
prepped = s.prepare_request(req)
r = s.send(prepped)
print(r.text)
"""


